package id.net.iconpln.bb.client.view;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;
import com.extjs.gxt.ui.client.widget.layout.FitData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.google.gwt.user.client.Element;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore; 
import id.net.iconpln.bb.client.data.TestData;
import id.net.iconpln.bb.client.model.PageBbDokBean;
import id.net.iconpln.bb.client.model.PageBbStockBean;
import id.net.iconpln.bb.client.model.PageBbSurveyorBean;
import id.net.iconpln.bb.client.model.Stock;
import java.util.ArrayList;  
import java.util.List;  

public class PageBbSurveyor extends LayoutContainer {

	Grid<PageBbSurveyorBean> grid;
	private FormData formData;
	private FormBinding formBindings; 
	
	@Override
	  protected void onRender(Element parent, int pos) {
	    super.onRender(parent, pos); 
	    setLayout(new FitLayout());  
	    
	    ContentPanel cp = new ContentPanel();  
	  
	    cp.setHeading("Master Surveyor");  
	    cp.setFrame(true);  
	    cp.setSize(1030, 600);  
	    cp.setLayout(new RowLayout(Orientation.HORIZONTAL));  
	  
	    grid = createGrid();
	    grid.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
	    grid.getSelectionModel().addListener(Events.SelectionChange,  
	            new Listener<SelectionChangedEvent<PageBbSurveyorBean>>() {  
	              public void handleEvent(SelectionChangedEvent<PageBbSurveyorBean> be) {  
	                if (be.getSelection().size() > 0) {  
	                  formBindings.bind((ModelData) be.getSelection().get(0));  
	                } else {  
	                  formBindings.unbind();  
	                }  
	              }  
	            });  
	    FormPanel panel = createForm();
	    formBindings = new FormBinding(panel, true);   
	  
	    cp.add(panel, new RowData(.4, 1));  
	    cp.add(grid, new RowData(.6, 1)); 
	    add(cp, new FitData(5)); 
	  }  
	  
	  private FormPanel createForm() {  
	    FormPanel panel = new FormPanel();  
	    panel.setHeaderVisible(false);
	    
	    FieldSet fieldSet2 = new FieldSet();  
	    fieldSet2.setHeading("Form Data Master Surveyor");  
	    fieldSet2.setCollapsible(false);

	    FormLayout layout2 = new FormLayout();  
	    layout2.setLabelWidth(150);  
	    fieldSet2.setLayout(layout2);
	    
	    final TextField<String> txtnama = new TextField<String>();
	    txtnama.setName("nama_surveyor");
	    txtnama.setFieldLabel("Nama Surveyor");  
	    fieldSet2.add(txtnama, formData);
	    
	    final TextArea txtalamat = new TextArea();
	    txtalamat.setName("alamat");
	    txtalamat.setFieldLabel("Alamat Surveyor");  
	    fieldSet2.add(txtalamat, formData);
	  
	    panel.add(fieldSet2);
	    
	    ContentPanel p = new ContentPanel();
	    p.setHeaderVisible(false);	    
	    p.setButtonAlign(HorizontalAlignment.RIGHT);  
	    p.addButton(new Button("Reset", new SelectionListener<ButtonEvent>() {  
	      @Override  
	      public void componentSelected(ButtonEvent ce) {  
	        grid.getStore().rejectChanges();
	        txtnama.setValue("");
	        txtalamat.setValue("");
	      }  
	    }));  
	    p.addButton(new Button("Submit", new SelectionListener<ButtonEvent>() {  
	      @Override  
	      public void componentSelected(ButtonEvent ce) {  
	        grid.getStore().commitChanges();
	        MessageBox.alert("Message", "DATABASE sedang dalam Perbaikan", null);
	      }  
	    }));  

	    panel.add(p);
	    return panel;  
	  }  
	  
	  private Grid<PageBbSurveyorBean> createGrid() {  
	    
	   
	    final List<ColumnConfig> columns = new ArrayList<ColumnConfig>(); 
	    
	    RowNumberer id = new RowNumberer();  
	    id.setWidth(30);
	    
	    ColumnConfig nama = new ColumnConfig("nama_surveyor", "Nama Surveyor", 200);
	    nama.setAlignment(HorizontalAlignment.LEFT);
	    
	    ColumnConfig alamat = new ColumnConfig("alamat", "Alamat Surveyor", 600);
	    alamat.setAlignment(HorizontalAlignment.LEFT);
	    
	    columns.add(id);
	    columns.add(nama);
	    columns.add(alamat);
	    
	   
	    ListStore<PageBbSurveyorBean> store = new ListStore<PageBbSurveyorBean>();  
	    store.setMonitorChanges(true);  
	    store.add(TestData.getSurveyor());  
	  
	    final ColumnModel cm = new ColumnModel(columns);
	    
	    grid = new Grid<PageBbSurveyorBean>(store, cm);
	    grid.setBorders(true);  
	    
	    return grid;  
	  }  
}

